import matplotlib.pyplot as plt
import numpy as np

# 设置支持 Unicode 的字体
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei','DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

def demonstrate_power_function_continuity():
    x = np.linspace(0.1, 3, 1000)
    alpha_values = [0.5, 1, 1.5, 2]  # 不同的指数
    
    plt.figure(figsize=(12, 8))
    
    for i, alpha in enumerate(alpha_values):
        y = x**alpha
        
        plt.subplot(2, 2, i+1)
        plt.plot(x, y, 'b-', linewidth=2, label=f'$y = x^{{{alpha}}}$')
        
        # 标记几个点验证连续性
        test_points = [0.5, 1.0, 2.0]
        for x0 in test_points:
            y0 = x0**alpha
            plt.scatter([x0], [y0], color='red', s=50)
            plt.annotate(f'({x0}, {y0:.2f})', (x0, y0), 
                        xytext=(10, 10), textcoords='offset points')
        
        plt.title(f'幂函数 $y = x^{{{alpha}}}$ 的连续性')
        plt.xlabel('x')
        plt.ylabel('y')
        plt.grid(True, alpha=0.3)
        plt.legend()
    
    plt.tight_layout()
    plt.show()
    
    # 验证极限运算
    x0 = 2
    for alpha in alpha_values:
        limit_left = (x0 - 0.001)**alpha
        limit_right = (x0 + 0.001)**alpha
        actual_value = x0**alpha
        
        print(f"幂函数 y = x^{alpha}:")
        print(f"  lim(x→{x0}-) y = {limit_left:.6f}")
        print(f"  lim(x→{x0}+) y = {limit_right:.6f}")
        print(f"  y({x0}) = {actual_value:.6f}")
        print(f"  连续性验证: {abs(limit_left - actual_value) < 1e-5 and abs(limit_right - actual_value) < 1e-5}")
        print()

demonstrate_power_function_continuity()